package com.goldmantis.alitrip.controller.authorization;

import com.goldmantis.alitrip.common.ReturnMessage;
import com.goldmantis.alitrip.common.annotation.Log;
import com.goldmantis.alitrip.common.sysenum.EnumLogCatagory;
import com.goldmantis.alitrip.common.sysenum.EnumOperationType;
import com.goldmantis.alitrip.pojo.SysRoleRes;
import com.goldmantis.alitrip.service.SysRoleResService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 12:31 2018/2/11
 */
@RestController
@Api(description = "角色菜单管理")
@RequestMapping("sys/role/res")
public class SysRoleResController {

    @Autowired
    private SysRoleResService sysRoleResService;


    @GetMapping(value = "{userRoleId}")
    @ApiOperation(value = "根据Id获取角色菜单", httpMethod = "GET")
    public ResponseEntity<ReturnMessage> getById(
            @ApiParam(value = "主键", required = true) @PathVariable(required = true) Long userRoleId) {
        return ResponseEntity.ok(ReturnMessage.success(sysRoleResService.selectById(userRoleId)));
    }

    /**
     * 新增用户
     * @param sysRoleRes
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "新增角色菜单映射", httpMethod = "POST", notes = "优先判断resIds属性，如果此属性有值，则会忽略resId属性，注意客户端需要传递systemid！")
    @Log(operationName = "新增角色菜单映射",logCatagory = EnumLogCatagory.operation,operationType = EnumOperationType.grant)
    public ResponseEntity<ReturnMessage> post(@ApiParam("角色菜单对象") @RequestBody @Valid SysRoleRes sysRoleRes) {
        sysRoleResService.insertBatch(sysRoleRes);
        return ResponseEntity.ok(ReturnMessage.success("保存成功!"));
    }

    /**
     * 编辑角色菜单
     *
     * @param sysRoleRes
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "编辑角色菜单", httpMethod = "PUT")
    public ResponseEntity<ReturnMessage> put(@ApiParam("角色菜单对象") @RequestBody SysRoleRes sysRoleRes) {
        try {
            return ResponseEntity.ok(ReturnMessage.success(sysRoleResService.update(sysRoleRes)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }

    /**
     * 批量删除角色菜单
     *
     * @param role_res_ids
     * @return
     */
    @DeleteMapping(value = "{role_res_ids}")
    @ApiOperation(value = "批量删除角色菜单", httpMethod = "DELETE")
    public ResponseEntity<ReturnMessage> delete(@ApiParam("角色菜单id 逗号隔开（1,2）") @PathVariable String role_res_ids) {
        try {
            List<Object> ids = Arrays.asList(StringUtils.split(role_res_ids, ","));
            return ResponseEntity.ok(ReturnMessage.success(sysRoleResService.deleteByIds(ids, SysRoleRes.class, "roleresid")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ReturnMessage.failure(e));
        }
    }


    @GetMapping(value = "getByRoleId/{roleId}")
    @ApiOperation(value = "根据角色Id获取用户菜单信息", httpMethod = "GET")
    @Log(operationName = "根据角色Id获取用户菜单信息", logCatagory = EnumLogCatagory.access, operationType = EnumOperationType.query)
    public ResponseEntity<ReturnMessage> getByRoleId(
            @ApiParam(value = "角色主键", required = true) @PathVariable(required = true) Long roleId) {
        return ResponseEntity.ok(ReturnMessage.success(sysRoleResService.selectByRoleId(roleId)));
    }


    @GetMapping(value = "getByResId/{resId}")
    @ApiOperation(value = "根据菜单Id反向获取用户菜单信息", httpMethod = "GET")
    @Log(operationName = "根据菜单Id反向获取用户菜单信息", logCatagory = EnumLogCatagory.access, operationType = EnumOperationType.query)
    public ResponseEntity<ReturnMessage> getByResId(
            @ApiParam(value = "菜单主键", required = true) @PathVariable Long resId) {
        return ResponseEntity.ok(ReturnMessage.success(sysRoleResService.selectByResId(resId)));
    }


}
